package ch.ethz.inf.shop;

import java.io.IOException;
import java.lang.reflect.Field;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ch.ethz.inf.shop.model.DatastoreInterface;
import ch.ethz.inf.shop.model.DatastoreInterfaceSimpleDatabase;
import ch.ethz.inf.shop.model.Product;
import ch.ethz.inf.shop.util.html.BeanTableHelper;

/**
 * Servlet implementation class of Products search page
 */
@WebServlet(description = "Page that displays the products based on different filtering criteria.", urlPatterns = { "/Products" })
public final class ProductsServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private final DatastoreInterface dbInterface = new DatastoreInterfaceSimpleDatabase();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected final void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {

		final HttpSession session = request.getSession(true);

		/*******************************************************
		 * Construct a table to present all our results
		 */
		final BeanTableHelper<Product> table = new BeanTableHelper<Product>("products", "productsTable", Product.class);

		for (Field f : Product.class.getDeclaredFields()) {
			Title titleAnnotation = f.getAnnotation(Title.class);
			if (titleAnnotation != null && titleAnnotation.isVisible()) {
				table.addBeanColumn(titleAnnotation.text(), f.getName());
			} else if (titleAnnotation == null) {
				table.addBeanColumn(f.getName(), f.getName());
			}
		}

		/*
		 * Column 4: This is a special column. It adds a link to view the
		 * Product. We need to pass the product identifier to the url.
		 */
		table.addLinkColumn("", "<img src=\"images/document-open.png\" alt=\"View Product\"/>", "Product?productId=", "productId");

		// Pass the table to the session. This will allow the respective jsp
		// page
		// to display the table.
		session.setAttribute("products", table);

		// The filter parameter defines what to show on the Products page
		final String filter = request.getParameter("filter");

		if (filter == null) {

			// If no filter is specified, then we display all the products!
			table.addObjects(this.dbInterface.getAllProducts());

		} else if (filter.equals("name")) {

			// Filter by Name
			final String name = request.getParameter("name");
			table.addObjects(this.dbInterface.getProductsByName(name));

		} else if (filter.equals("category")) {

			final String name = request.getParameter("name");
			table.addObjects(this.dbInterface.getProductsByCategoryName(name));

		} else if (filter.equals("brand")) {

			final String name = request.getParameter("name");
			table.addObjects(this.dbInterface.getProductsByBrandName(name));

		} else if (filter.equals("popular")) {

			table.addObjects(this.dbInterface.getProductsByPopularity());

		} else {

			throw new RuntimeException("Code should not be reachable!");

		}

		// Finally, proceed to the Products.jsp page which will render the
		// Products
		this.getServletContext().getRequestDispatcher("/Products.jsp").forward(request, response);

	}
}
